"""
red:警报         31m
green:系统提示    32m
yellow:遍历展示   33m
blue:选项        34m
青:输入           96m
"""
from endOfPeriodProject.student.student import *


class StudentManager(object):
    def __init__(self):
        self.stu_list = []

    def run(self):
        self.load()
        while True:
            try:
                self.show_question()
                answer = int(input("\033[96m Please enter the number what you want to use: \033[0m"))
                if answer == 1:
                    # Query all student
                    self.query_all_student()
                elif answer == 2:
                    # Query one student
                    self.query_one_student()
                elif answer == 3:
                    # Insert a new student
                    self.insert_student()
                elif answer == 4:
                    # Remove a student
                    self.remove_student()
                elif answer == 5:
                    # Update a student's information
                    self.update_student()
                elif answer == 6:
                    # Save student information into student data
                    self.save_to_data()
                elif answer == 7:
                    # Edit
                    print("\033[47m ------Thank you for your ues!!!------ \033[0m")
                    break
                else:
                    print("\033[31m Unknown answer, please enter again\033[0m")
                    self.show_question()
                    self.run()
            except ValueError:
                print("\033[31m Unknown answer, please enter again\033[0m")
                self.show_question()
                self.run()

    def show_question(self):
        print("\033[34m 1.Query all student\033[0m\n"
              "\033[34m 2.Query one student\033[0m\n"
              "\033[34m 3.Insert a new student\033[0m\n"
              "\033[34m 4.Remove a student\033[0m\n"
              "\033[34m 5.Update a student's information\033[0m\n"
              "\033[34m 6.Save student information into student data\033[0m\n"
              "\033[34m 7.Edit\033[0m")

    def query_all_student(self):
        if len(self.stu_list) == 0:
            print("\033[31m !!!List is null, no data here!!! \033[0m")
        else:
            for i in self.stu_list:
                print(f"\033[33m{i}\033[0m")

    def insert_student(self):
        name = input("\033[96m ---Please enter your name: \033[0m")
        age = input("\033[96m ---Please enter your age: \033[0m")
        gender = input("\033[96m ---Please enter your gender: \033[0m")
        tel = input("\033[96m ---Please enter your telephone number: \033[0m")
        new_stu = Student(name, age, gender, tel)
        self.stu_list.append(new_stu)
        print("\033[32m This is the information of this student's\033[0m ")
        self.query_all_student()

    def remove_student(self):
        counter = 0
        if len(self.stu_list) == 0:
            print("\033[31m !!!List is null, no data here!!! \033[0m")
        else:
            remove_stu = input("\033[96m Please enter the name of student what you want to remove: \033[0m")
            for i in self.stu_list:
                counter += 1
                if i.name == remove_stu:
                    counter -= 1
                    self.stu_list.remove(i)
                    if counter != self.stu_list.__len__():
                        print("\033[31m !!!Not fount this student!!! \033[0m")
            print("\033[32m List after removed: \033[0m")
            self.query_all_student()

    def update_student(self):
        counter = 0
        if len(self.stu_list) == 0:
            print("\033[31m !!!List is null, no data!!! \033[0m")
        else:
            update_student = input("\033[96m Please enter the student's name what you want to update that: \033[0m")
            for i in self.stu_list:
                counter += 1
                if i.name == update_student:
                    counter -= 1
                    i.name = input("\033[96m ---Please enter the changed student's name: \033[0m")
                    i.age = input("\033[96m ---Please enter the changed student's age: \033[0m")
                    i.gender = input("\033[96m ---Please enter the changed student's gender: \033[0m")
                    i.tel = input("\033[96m ---Please enter the changed student's telephone number: \033[0m")
                    print(f"\033[33m {i} \033[0m")
                    if counter == self.stu_list.__len__():
                        print("\033[31m !!!Not find this student!!! \033[0m")

    def query_one_student(self):
        if len(self.stu_list) == 0:
            print("\033[31m !!!List is null, no data!!! \033[0m")
        else:
            this_student = input("\033[96m Please enter that student's name what you want query: \033[0m")
            for i in self.stu_list:
                if i.name == this_student:
                    print(f"\033[33m {i} \033[0m")
                else:
                    print("\033[31m !!!Not find this student!!! \033[0m")

    def save_to_data(self):
        # 打开/创建一个文件
        f = open("student.data", "w", encoding="utf-8")
        # 把列表数据转换成字典
        new_list = [i.__dict__ for i in self.stu_list]
        # 把字典数据转换成字符串
        a = str(new_list)
        # 写入
        f.write(a)
        # 关闭
        f.close()

    def load(self):
        try:
            f = open("../student/student.data", "r", encoding="utf-8")
        except:
            f = open("../student/student.data", "w", encoding="utf-8")
        else:
            # 读取
            data = f.read()
            # eval(data) 把字符串转换为字典
            new_list = eval(data)
            self.stu_list = [Student(i["name"], i["age"], i["gender"], i["tel"]) for i in new_list]
            f.close()
